<?php

/**
 * The meat and butter of Hinagiku. 
 *
 * @package OWL
 * @subpackage Core
 * @since 1.5
 * @author KeiMOon
 */

class Owl_Core {
    /**
     * Default layout file
     *
     * @access private
     * @var string $default_layout
     */
    private $default_layout = "layout.php";

    /**
     * Default error template
     *
     * @var string error_template
     */
    private $error_template = "error";

    /**
     * Application rendering method
     */
    function dispatch() {
        $template_instance = $this->_create_template_instance();
        $template_instance->render($this->default_layout);
    }

    /**
     * Method for setting the default layout file. Default is "layout.php"
     *
     * @param string $layout Default layout file. 
     */
    function set_layout_file($layout) {
        if (!empty ($layout)) {
            $this->default_layout = $layout;
        }
    }

    /**
     * Method for setting the default error template. Default is "error"
     *
     * @param string $template Default error template
     */
    function set_error_template($template) {
        if (!empty ($template)) {
            $this->error_template = $template;
        }
    }

    /**
     * Create the template instance based on the query string
     *
     * @return Template object instance
     */
    private function _create_template_instance() {
        if (!$this->_check_template_file($_GET["template"])) {
            if (!$this->_check_template_file($this->error_template)) {
                Owl_Functions::hngk_die("<b>Critical error</b>: Cannot find provided template: $template_file!!!");
            }
            else {
                return Owl_System_Helper::create_instance($this->_get_template_class($this->error_template));
            }
        }
        else {
            return Owl_System_Helper::create_instance($this->_get_template_class($_GET["template"]));
        }
    }

    private static function _check_template_file($template) {
        $template = trim($template);
        $file =  ($template == "" || $template == "home") ? "template-" . get_option('home_template', 'note') . '.php' : "template-" . $template . ".php";
        if (file_exists(Owl_Theme_Control::get_current_theme_path() . $file)) {
            set_include_path(get_include_path() . PATH_SEPARATOR . Owl_Theme_Control::get_current_theme_path());
            return true;
        }
        elseif (file_exists(TEMPLATE_PATH . $file)) {
            set_include_path(get_include_path() . PATH_SEPARATOR . TEMPLATE_PATH);
            return true;
        }
        else {
            return false;
        }
    }

    private function _get_template_class($template) {
        $template = trim($template);
        $template_class = ($template == "" || $template == "home") ? "Template_".str_replace(" ", "_", ucwords(str_replace("-", " ", get_option('home_template', 'note')))) : "Template_".str_replace(" ", "_", ucwords(str_replace("-", " ", $template)));
        return $template_class;
    }
}
